import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


@allure.feature('TestCase_12: 桌面播放器收藏功能正常')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001043372?url_cache_key=17e362af8b12add5e45c5e468a11c15a')
class TestCaseMusic12:
    """
    预期结果: 列表展示从最新桌面播放器收藏的内容
    """

    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()

        self.music_choice_page = CommonPage(self.driver).get_music_choice_page()
        self.music_index_page = CommonPage(self.driver).get_music_index_page()
        self.music_player_page = CommonPage(self.driver).get_music_player_page()
        self.music_my_collection_page = CommonPage(self.driver).get_music_my_collection_page()

    def teardown_class(self):
        XiaoMaUtils.close_app('music')
        self.driver.quit()

    @allure.story("step1: 跳过欢迎页, 播放任意列表音乐, 进入播放器页面")
    @pytest.mark.run(order=121)
    def test_music_12_1_enter_player_page(self):
        try:
            # 进入音乐
            self.launcher_index_page.click_app_list()
            self.launcher_app_list_page.click_appname(TestCase.MUSIC)
            if self.music_choice_page.is_display_title():
                self.music_choice_page.click_skip()

            self.music_index_page.click_list_music(1)
            self.music_index_page.click_thumb_info()
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step2: 检查收藏前后的收藏icon")
    @pytest.mark.run(order=122)
    def test_music_12_2_check_collection(self):
        try:
            # 暂停播放
            self.music_player_page.click_play()
            # 获取当前播放的歌曲名
            self.music_player_page.get_ele_text(self.music_player_page.get_song_name())
            self.music_player_page.screenshot_part(self.music_player_page.get_collection_icon(), 'collcetion_icon1')
            self.music_player_page.click_collection()
            # 获取toast的截图
            XiaoMaUtils.get_screencap(png_name="testcase12_collection_toast.png")

            # 先去掉toast的检验, 因为截图占用时间较长, 再去判断会获取不到文案
            # toast_flag = self.music_player_page.is_toast_exist(text="收藏成功")
            time.sleep(2)
            self.music_player_page.screenshot_part(self.music_player_page.get_collection_icon(), 'collcetion_icon2')

            # 返回到音乐首页
            self.music_player_page.click_player_back()

            contrast_res = self.music_player_page.image_contrast('collcetion_icon1.png', 'collcetion_icon2.png')
            assert (contrast_res > 0)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)
            file1 = open('Image/collcetion_icon1.png', 'rb').read()
            allure.attach(file1, '未收藏时的icon', allure.attachment_type.PNG)
            file2 = open('Image/testcase12_collection_toast.png', 'rb').read()
            allure.attach(file2, '收藏的toast', allure.attachment_type.PNG)
            file3 = open('Image/collcetion_icon2.png', 'rb').read()
            allure.attach(file3, '收藏时的icon', allure.attachment_type.PNG)

    @allure.story("step3: 查看收藏页面是否有数据")
    @pytest.mark.run(order=123)
    def test_music_12_3_check_my_collection_page_collection(self):
        try:
            self.music_index_page.click_my_music()
            # 进入收藏页面, 检查是否有收藏数据
            assert self.music_my_collection_page.get_collection_list_music_title(1)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)


if __name__ == '__main__':
    pass
